import { Table } from 'antd';
import Cls from 'classnames';
import { AcquisitionManageContext, IAcquisitionActionEnum } from '../mconfig';
import { useCallback, useContext, useEffect, useMemo, useState } from 'react';
import { openServiceColumns } from '../mconfig';
import { useAcquisitionManageReducer } from '../mconfig';
import { getLoadMapList, getSelectDataMapList, getTenantID } from '@/utils';
import { ILoadMapTypeAll } from '@/common';

export const UpdateServiceView: React.FC<any> = ({ addMaterialModalStatus, editTable, openMaterialModalStatus, formName, pageType, isHaveContract }) => {
  // /**  获取数据字典 */
  const { loadMapInfo, mapLoading } = getLoadMapList([
    `${getTenantID()}${ILoadMapTypeAll.MATERIAL_TYPE}`, //类型
    `${getTenantID()}${ILoadMapTypeAll.MATERIAL_BRAND}`, //品牌
    `${getTenantID()}${ILoadMapTypeAll.MATERIAL_MIDI}`, //单位
  ]);
  // /** 类型 */
  const assetTypeList = getSelectDataMapList(loadMapInfo?.[`${getTenantID()}${ILoadMapTypeAll.MATERIAL_TYPE}`], 'name', 'id', true);
  // 品牌
  const brandTypeList = getSelectDataMapList(loadMapInfo?.[`${getTenantID()}${ILoadMapTypeAll.MATERIAL_BRAND}`], 'name', 'id', true);
  // 单位
  const midiTypeList = getSelectDataMapList(loadMapInfo?.[`${getTenantID()}${ILoadMapTypeAll.MATERIAL_MIDI}`], 'name', 'id', true);

  const { serverList, acquisitionDispatch } = useContext(AcquisitionManageContext);

  const delSelectRow = (record) => {
    // 取消选择的合同
    const arr = serverList.filter((item) => {
      return item.id !== record.id;
    });
    acquisitionDispatch({
      type: IAcquisitionActionEnum.SELECT_SERVER,
      payload: arr,
    });
  };

  // 修改表格内容
  const onEditTable = ({ type, value, index }) => {
    const tempList = [...serverList];

    // 服务人员及联系方式
    if (type == 'userOrPhone') {
      tempList[index].userOrPhone = value;
    }

    acquisitionDispatch({
      type: IAcquisitionActionEnum.SELECT_SERVER,
      payload: tempList,
    });
  };

  /** 表格行配置 */
  const columns: Record<string, any>[] = useMemo(() => {
    return openServiceColumns(addMaterialModalStatus, editTable, openMaterialModalStatus, delSelectRow, onEditTable, pageType, isHaveContract);
  }, [serverList]);

  // useEffect(() => {}, [serverList]);

  return (
    <div className={Cls('list-table', 'descriptionsType-tableSty')} style={{ marginTop: '10px' }}>
      <Table bordered size="small" dataSource={serverList} columns={columns} rowKey={(res) => res?.id} scroll={{ x: 1200, y: '' }} pagination={false} />
    </div>
  );
};
